package com.eqdd.yiqidian.ui.mine.shenqing

import android.content.Intent
import com.amap.api.maps2d.model.LatLng
import com.eqdd.nextinputs.StaticScheme
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimTimeSelectBean
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.map.SelectMapActivity
import com.lzy.okgo.model.HttpParams
import org.jetbrains.anko.startActivityForResult

/**
 * 出差申请
 * Created by jhj on 18-6-11.
 */
class ApplyBusinessActivity : BaseApplyActivity() {

    private var latlon: String? = null

    override val mTitle: String
        get() = "出差申请"
    override val url: String
        get() = HttpConfig.BASE_URL + HttpConfig.CREATE_TRAVEL
    override val isRequestLeader: Boolean
        get() = true
    override val leaderIndex: Int?
        get() = 9

    override fun uploadParams(): HttpParams {
        params.put("travelStartTime", setParam(4))
        params.put("travelEndTime", setParam(5))
        params.put("travelTimes", setParam(6))
        params.put("travelReason", setParam(7))
        params.put("travelAddress", setParam(8))
        params.put("mapAddress", dealText(latlon))
        return params
    }


    override fun itemClick(p: Int) {
        when (p) {
            4 -> {
                isUpper = true
                openTimePicker(4, 5, true)
            }
            5 -> {
                isUpper = false
                openTimePicker(4, 5, true)
            }
            8 -> startActivityForResult<SelectMapActivity>(Config.SELECT_MAP)
        }
    }

    override fun setEditBeans() {
        super.setEditBeans()
        datas.add(SlimTimeSelectBean("开始时间", "", 4, "请选择").setStartTime(true).setNeed(true))
        datas.add(SlimTimeSelectBean("结束时间", "", 5, "请选择").setStartTime(false).setNeed(true))
        datas.add(SlimEditEnterBean("出差时长(天)", "", 6, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true))
        datas.add(SlimEditEnterBean("出差原因", "", 7, "请输入").setNeed(true))
        datas.add(SlimEditEnterBean("出差地点", "", 8, true, "请选择").setNeed(true))
        datas.add(SlimEditHintBean("审批人", "", 9))
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null)
            return
        if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_MAP) {
            val address = data.getStringExtra(Config.MAP_RESULT_STRING)
            val latLng = data.getParcelableExtra<LatLng>(Config.MAP_RESULT_LOCATION)
            latlon = latLng.latitude.toString() + "," + latLng.longitude
            slimAdapterEx.getDataItem(8).content = address
            slimAdapterEx.notifyItemChanged(8)
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}